﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace Atendex.Web.AppCode
{
    public static class ListBoxHelper
    {
        public static void TransfereListItems(ListBox from, ListBox to)
        {
            int[] indices = from.GetSelectedIndices();
            if (indices.Length == 0) return;

            var selecionados = new List<ListItem>(indices.Length);
            foreach (int i in indices)
            {
                var item = from.Items[i];
                selecionados.Add(item);
            }

            foreach (ListItem item in selecionados)
            {
                from.Items.Remove(item);
                to.Items.Add(item);
            }

            from.ClearSelection();
            to.ClearSelection();
        }

        public static void TransfereItemsDeLista(ListBox from, ListBox to, IEnumerable<string> values)
        {
            var items = new List<ListItem>();
            foreach (string value in values)
            {
                foreach (ListItem item in from.Items)
                {
                    if (item.Value != value) continue;

                    items.Add(item);
                    from.Items.Remove(item);
                    break;
                }
            }
            to.Items.AddRange(items.ToArray());
        }

        public static void SelecionaItem(DropDownList lista, string valor)
        {
            lista.ClearSelection();
            foreach (ListItem item in lista.Items)
            {
                if (item.Value == valor)
                {
                    item.Selected = true;
                    return;
                }
            }
        }

        public static void TransfereTudo(ListBox de, ListBox para)
        {
            foreach (ListItem item in de.Items)
            {
                para.Items.Add(item);
            }
            de.Items.Clear();
        }
    }
}
